package com.zhongmeng.rxjava2test

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView

import com.zhongmeng.rxjava2test.bean.LoginBean
import com.zhongmeng.rxjava2test.presenter.LoginPresenter
import com.zhongmeng.rxjava2test.presenter.LoginPresenterImpl
import com.zhongmeng.rxjava2test.utils.LogUtils
import com.zhongmeng.rxjava2test.utils.SaveUserInfo
import com.zhongmeng.rxjava2test.utils.UIUtils
import com.zhongmeng.rxjava2test.view.LoginView

import butterknife.OnClick
import kotterknife.bindView

class MainActivity : AppCompatActivity(), LoginView<Any>,View.OnClickListener {

    //@BindView(R.id.edit_account)
    val editAccount: EditText by bindView(R.id.edit_account)
    val editPassword: EditText by bindView(R.id.edit_password)
    val forget: TextView by bindView(R.id.forget)
    val login: Button by bindView(R.id.login)
//    @BindView(R.id.edit_password)
//    internal var editPassword: EditText? = null
//    @BindView(R.id.forget)
//    internal var forget: TextView? = null
//    @BindView(R.id.login)
//    internal var login: Button? = null

    private var username = ""
    private var password = ""
    private val GET_LOGIN = 0

    private var loginPresenter: LoginPresenter? = null//mvp中的中间量，协调modle和view

    override val userName: String
        get() {
            username = editAccount!!.text.toString().trim { it <= ' ' }
            return username
        }

    override val passWord: String
        get() {
            password = editPassword!!.text.toString().trim { it <= ' ' }
            return password
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        //ButterKnife.bind(this)
        //editAccount!!.setText("ssss")
        forget.setOnClickListener(this)
        login.setOnClickListener(this)
        loginPresenter = LoginPresenterImpl(this)//获取presenter的实例，this是view的实例
    }


    @OnClick(R.id.forget, R.id.login)
    override fun onClick(view: View) {
        when (view.id) {
            R.id.forget -> {
            }
            R.id.login -> loginPresenter!!.login(GET_LOGIN)//登录按钮触发presenter中的方法
        }
    }

    override fun showProgress() {

    }

    override fun hideProgress() {

    }

    override fun setUserNameError() {
        UIUtils.toast("请输入手机号码")
    }

    override fun setPassWordError() {
        UIUtils.toast("请输入密码")
    }

    override fun onFail(throwable: Throwable) {
        LogUtils.z(throwable.message!!)
        UIUtils.toast("服务器连接失败")
    }

    override fun onSuccess(data: Any, whih: Int) {
        val loginBean = data as LoginBean
        //保存用户数据
        SaveUserInfo.instance.saveAllData(loginBean)
        LogUtils.z("loginBean:" + loginBean)
        val code = loginBean.code
        if (code == 200) {
            LogUtils.z("我是:" + loginBean.userinfo!!.username!!)
            val intent = Intent(this, HomeActivity::class.java)
            startActivity(intent)
            finish()
        } else
            UIUtils.toast(loginBean.message!!)
    }

}
